library(ggplot2) 
library(sf) 
library(gridExtra)

map_2016 <- st_read("liveuamap_2016-01-01.kml")
map_2018 <- st_read("liveuamap_2018-01-01.kml") #Names need to change
map_2020 <- st_read("liveuamap_2020-01-01.kml")

cbp <- c("firebrick2","#000000", "#F0E442","olivedrab4")

syria_2016 <- ggplot(st_zm(map_2016)) + 
  geom_sf(aes(fill=Name)) +
  #labs(caption="January 1, 2020") + 
  ggtitle("January 1, 2016") +  
  scale_fill_manual(name="", values = cbp,
                    labels = c("Government", "ISIS", "SDF","Opposition")) +  
  theme_classic() +theme(axis.line=element_blank(),axis.text.x=element_blank(),
                         axis.text.y=element_blank(),axis.ticks=element_blank(),
                         axis.title.x=element_blank(),
                         axis.title.y=element_blank(),legend.position="none",
                         plot.title = element_text(hjust = 0.5, size = 14, family = "LM Roman 10"))

cbp2 <- c("firebrick2", "#56B4E9", "purple4","#000000", 
          "#F0E442","olivedrab4","olivedrab3")

map_2018$Name[which(map_2018$Name=="Golan Heights")] <- "Golan_Heights"

syria_2018 <- ggplot(st_zm(map_2018)) + 
  geom_sf(aes(fill=Name)) + ggtitle("January 1, 2018") +
  scale_fill_manual(name="", values = cbp2,
                    labels = c("Government", "Golan Heights", "HTS","ISIS","SDF","Opposition","Turkey")) +  
  theme_classic() +theme(axis.line=element_blank(),axis.text.x=element_blank(),
                         axis.text.y=element_blank(),axis.ticks=element_blank(),
                         axis.title.x=element_blank(),
                         plot.title = element_text(hjust = 0.5, size = 14, family = "LM Roman 10"),
                         axis.title.y=element_blank(),legend.position="none")

cbp1 <- c("firebrick2", "#56B4E9", "purple4","#000000", 
          "olivedrab4","#F0E442","olivedrab3")
syria_2020 <- ggplot(st_zm(map_2020)) + 
  geom_sf(aes(fill=Name)) +
  #labs(caption="January 1, 2020") + 
  ggtitle("January 1, 2020") +  
  scale_fill_manual(name="", values = cbp1,
                    labels = c("Government", "Golan Heights", "HTS","ISIS","Opposition","SDF","Turkey")) +  
  theme_classic() +theme(axis.line=element_blank(),axis.text.x=element_blank(),
                         axis.text.y=element_blank(),axis.ticks=element_blank(),
                         axis.title.x=element_blank(),
                         axis.title.y=element_blank(),
                         plot.title = element_text(hjust = 0.5, size = 14, family = "LM Roman 10"),
                         legend.text=element_text(size=12, family = "LM Roman 10"),
                         legend.title=element_text(size=12, family = "LM Roman 10"))

g_legend<-function(a.gplot){
  tmp <- ggplot_gtable(ggplot_build(a.gplot))
  leg <- which(sapply(tmp$grobs, function(x) x$name) == "guide-box")
  legend <- tmp$grobs[[leg]]
  return(legend)}

mylegend<-g_legend(syria_2020)

A <- grid.arrange(arrangeGrob(syria_2016,syria_2018,syria_2020+theme(legend.position="none"),
                              ncol=3),mylegend, nrow=1,widths=c(5, 1)) 

ggsave("Maps_161820.pdf", plot = A, 
       device = cairo_pdf, width=9.83, height=3, units = "in")  